<%--
  Created by IntelliJ IDEA.
  User: zzz
  Date: 2020/2/4
  Time: 16:19
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
</head>
<body>
<div class="card">
    <div class="card-header">
        <span style="margin-right: 10px;">颜色分类</span>
        <button onclick="initialization()" type="button" class="btn btn-primary btn-uppercase" data-toggle="modal" data-target="#insertFK"><i class="ti-plus"></i></button>
    </div>
    <div id="myFK" class="card-body">

    </div>
</div>



<div class="modal fade bd-example-modal-lg" id="insertFK" tabindex="-1" role="dialog" aria-labelledby="insertFKTitle" aria-hidden="true">
    <div class="modal-dialog modal-dialog-centered modal-lg" role="document">
        <div class="modal-content">
            <div class="modal-header">
                <h5 class="modal-title" id="insertFKTitle">添加颜色分类</h5>
                <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                    <span aria-hidden="true">&times;</span>
                </button>
            </div>
            <div class="modal-body">
                <form id="furnitureKindsInfoForInsert">
                    <input type="hidden" name="f_fid" value="${requestScope.furniture.fid}">
                    <input type="hidden" name="sales" value="0">
                    <div class="form-group row">
                        <label for="fknameForInsert" class="col-sm-2 col-form-label">种类名称</label>
                        <div class="col-sm-4">
                            <input type="text" class="form-control" id="fknameForInsert" name="fkname" required/>
                            <div class="invalid-feedback"></div></div>
                        <label for="fkpriceForInsert" class="col-sm-2 col-form-label">种类价格</label>
                        <div class="col-sm-4">
                            <input type="text" class="form-control" id="fkpriceForInsert" name="fkprice" required/>
                            <div class="invalid-feedback"></div></div>
                    </div>
                    <div class="form-group row">
                        <label for="costForInsert" class="col-sm-2 col-form-label">种类成本</label>
                        <div class="col-sm-4">
                            <input type="text" class="form-control" id="costForInsert" name="cost" required/>
                            <div class="invalid-feedback"></div></div>
                        <label for="stockForInsert" class="col-sm-2 col-form-label">种类库存</label>
                        <div class="col-sm-4">
                            <input type="text" id="stockForInsert" name="stock" class="form-control"  required/>
                            <div class="invalid-feedback"></div></div>
                    </div>
                </form>
                <label class="col-form-label">请上传一张种类展示图:</label>
                <form style="" action="${APP_PATH}/furnitureKinds/fkfaceUpload" class="dropzone"></form>

            </div>
            <div class="modal-footer">
                <button id="cancel" type="button" class="btn btn-secondary" data-dismiss="modal">算了...</button>
                <input  type="submit" class="btn btn-primary" onclick="return insertFK()" value="决定了！"/>
            </div>
        </div>
    </div>
</div>
</body>
</html>

<script>
function initialization() {
    setFace(2);
    if(myDropzone != null){
        myDropzone.removeAllFiles();
    }
    $("#furnitureKindsInfoForInsert")[0].reset();
    $("#furnitureKindsInfoForInsert").removeClass().find("input[required]").each(function () {
        $(this).removeClass("is-valid is-invalid");
    });
}
    function insertFK() {
        var flag = true;
        $("#furnitureKindsInfoForInsert").find("input[required]").each(function () {
            var id = "#"+$(this).attr("id");
            if(validationNull(id)){
                flag = false;
            }
        });
        if(validationExist("fknameForInsert")){
            flag = false;
        }
        if(validationNumAndDecimal("#fkpriceForInsert",false,"b")){
            flag = false;
        }
        if(validationNumAndDecimal("#costForInsert",false,"b")){
            flag = false;
        }
        if(validationNumAndDecimal("#stockForInsert",true,'k')){
            flag = false;
        }
        var f;
        f = $("#insertFK").find(".dz-image").html();
        if(f == undefined){
            swal("您忘了上传展示图了哦~！", {icon: "warning"});
            flag = false;
        }

        if(flag){
            var data = $("#furnitureKindsInfoForInsert").serialize();
            
        $.ajax({
            url : "${APP_PATH}/furnitureKinds/insertFurnitureKinds",
            method : "POST",
            data : data,
            success : function (result) {
                if(result.success){
                    swal("添加成功！", {icon: "success"});
                }else {
                    swal("添加失败！", {icon: "error"});
                }
            },
            complete : function () {
                $("#cancel").click();
                getFKList();
            }
        });
        }
        return false;
    }
    
    function updateFKInfo(id) {
        $.ajax({
            url : "${APP_PATH}/furnitureKinds/updateInfo",
            data : $("#"+id).serialize(),
            method : "POST",
            success : function (result) {
                if(result.success){
                    swal("修改成功！", {icon: "success"});
                }else {
                    swal("修改失败！", {icon: "error"});
                }
            },
            complete : function () {
                getFKList();
            }
        });
    }

function updateFKFace(id,fkid) {
    var f;
    f = $("#" + id).find(".dz-image").html();
    if(f != undefined){
        $.ajax({
            url : "${APP_PATH}/furnitureKinds/updateFace",
            method : "POST",
            data : {
                fkid : fkid
            },
            success: function (result) {
                if(result.success){
                    swal("修改成功！", {icon: "success"}).then((value) => {
                        myDropzone.removeAllFiles();
                    getFKList();
                });
                }else{
                    swal("修改失败！", {icon: "error"});
                }
            }
        });
    }
}

function deleteFK(fkid) {

    swal({
        title: "确定要删除吗?",
        icon: "warning",
        buttons: ["算了", "就要"],
        dangerMode: true
    }).then((willDelete) => {
        if (willDelete) {

            $.ajax({
                url: "${APP_PATH}/furnitureKinds/deleteFurnitureKinds",
                method: 'POST',
                data: {
                    fkid : fkid
                },
                success: function (result) {
                    if (result.success) {
                        swal("删除成功！", {
                                icon: "success"
                            }
                        ).then((value) => {
                            getFKList();
                    });
                    }else{
                        swal("该分类中还存在交易信息，无法删除！", {
                                icon: "error"
                            }
                        );
                    }
                }
            });

        } else {
            swal("您取消了删除.", {
        icon: "info"
    });
}
});


}

    function getFKList() {
        $.ajax({
            url : "${APP_PATH}/furniture/getFurnitureDetail",
            method : "POST",
            data : {
                fid : ${requestScope.furniture.fid}
            },
            beforeSend : function () {
                var FKContent = "";
                FKContent += '<div style="margin: auto 0; margin-left: 555px;" class="spinner-grow text-info" role="status">';
                FKContent += '<span class="sr-only"></span>';
                FKContent += '</div>';
                $("#FKList").html(FKContent);
            },
            success : function (result) {
                if(result.success){
                    var FKList = result.data.furnitureKindsList;
                    var FKContent = "";
                    if(FKList.length == 0){
                        FKContent += '<div style=" margin : 0 auto;" class="col-md-2 m-b-15"><img style="width = 500px;" src="${APP_PATH}/img/nodata.png" class="img-fluid" alt="..."></div>';
                    }else{
                        FKContent += ' <div  class="card-columns">';
                        for(var i = 0; i < FKList.length; i++){

                            FKContent += '<div class="card FK">';
                            FKContent += '<div class="card-body">';
                            FKContent += '<figure><a onclick="setFace('+(i+3)+')"  data-toggle="modal" data-target="#fkface'+i+'"><img id="fkface" src="${APP_PATH}/'+FKList[i].fkface+'" class="card-img-top" alt="..."></a></figure>';
                            FKContent += '<form id="furnitureKindsInfo'+i+'">';
                            FKContent += '<input type="hidden" name="fkid" value="'+FKList[i].fkid+'">';
                            FKContent += '<div class="form-group row">';
                            FKContent += '<label for="fkname'+i+'" class="col-sm-2 col-form-label">名称</label>';
                            FKContent += '<div class="col-sm-10">';
                            FKContent += '<input type="text" class="form-control" id="fkname'+i+'" name="fkname" value="'+FKList[i].fkname+'" required/>';
                            FKContent += '<div class="invalid-feedback"></div></div></div>';
                            FKContent += '<div class="form-group row">';
                            FKContent += '<label for="fkprice'+i+'" class="col-sm-2 col-form-label">价格</label>';
                            FKContent += '<div class="col-sm-4">';
                            FKContent += '<input type="text" class="form-control" id="fkprice'+i+'" name="fkprice" value="'+FKList[i].fkprice.toFixed(2)+'" required/>';
                            FKContent += '<div class="invalid-feedback"></div></div>';
                            FKContent += '<label for="cost'+i+'" class="col-sm-2 col-form-label">成本</label><div class="col-sm-4">';
                            FKContent += '<input type="text" class="form-control" id="cost'+i+'" name="cost" value="'+FKList[i].cost.toFixed(2)+'" required/>';
                            FKContent += '<div class="invalid-feedback"></div></div></div>';
                            FKContent += '<div class="form-group row">';
                            FKContent += '<label for="stock'+i+'" class="col-sm-2 col-form-label">库存</label>';
                            FKContent += '<div class="col-sm-4">';
                            FKContent += '<input type="text" id="stock'+i+'" name="stock" class="form-control"  value="'+FKList[i].stock+'" required/><div class="invalid-feedback"></div></div>';
                            FKContent += '<label for="sales_FK'+i+'" class="col-sm-2 col-form-label">销量</label>';
                            FKContent += '<div class="col-sm-4">';
                            FKContent += '<input type="text" class="form-control" id="sales_FK'+i+'" name="sales" value="'+FKList[i].sales+'" readonly/>';
                            FKContent += '<div class="invalid-feedback">';
                            FKContent += '</div></div></div>';
                            FKContent += '<div class="form-group row">';
                            FKContent += '<div class="col-sm-12">';
                            FKContent += '<button style="float: right; " onclick="deleteFK('+FKList[i].fkid+')"  type="button" class="btn btn-danger " ><i class="ti-trash"></i><span style="margin-left: 10px">删除</span></button>';
                            FKContent += '<button style="float: right; margin-right: 20px;" onclick="updateFKInfo(\'furnitureKindsInfo'+i+'\')"  type="button" class="btn btn-info " ><i class="ti-save"></i><span style="margin-left: 10px">保存</span></button>';
                            FKContent += '</div></div></form></div></div>';

                            FKContent +='<div class="modal fade bd-example-modal-sm" id="fkface'+i+'" tabindex="-1" role="dialog" aria-labelledby="fkface'+i+'Title" aria-hidden="true">';
                            FKContent +='<div class="modal-dialog modal-dialog-centered modal-sm" role="document">';
                            FKContent +='<div class="modal-content">';
                            FKContent +='<div class="modal-header">';
                            FKContent +='<h5 class="modal-title" id="fkface'+i+'Title">修改颜色分类展示图</h5>';
                            FKContent +='<button type="button" class="close" data-dismiss="modal" aria-label="Close">';
                            FKContent +='<span aria-hidden="true" onclick="updateFKFace(\'fkface'+i+'\','+FKList[i].fkid+')">&times;</span>';
                            FKContent +='</button>';
                            FKContent +='</div>';
                            FKContent +='<div class="modal-body">';
                            FKContent +='<label class="col-form-label">请上传一张颜色分类展示图:</label>';
                            FKContent +='<form id="myDropzone'+i+'" action="${APP_PATH}/furnitureKinds/fkfaceUpload" class="dropzone">';
                            FKContent +='</form>';
                            FKContent +='</div>';
                            FKContent +='<div class="modal-footer">';
                            FKContent +='<button onclick="updateFKFace(\'fkface'+i+'\','+FKList[i].fkid+')"  type="button" class="btn btn-primary" data-dismiss="modal">ojbk</button>';
                            FKContent +='</div>';
                            FKContent +='</div>';
                            FKContent +='</div>';
                            FKContent +='</div>';
                            fileMap.set((i+3),null);
                        }
                        FKContent += '</div>';
                    }
                    $("#myFK").html(FKContent);
                    for(var i = 0; i < FKList.length; i++){
                        new Dropzone("#myDropzone"+i, {});
                    }
                }
            },
            complete : function () {
                //getBgColor("#myFK");
                validationRender();
                getFurnitureDetail();
            }
        });
    }
</script>